package com.hyacinth.lotterysystem.service.activityStatus.operater;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hyacinth.lotterysystem.dao.entity.ActivityDO;
import com.hyacinth.lotterysystem.dao.entity.ActivityPrizeDO;
import com.hyacinth.lotterysystem.dao.mapper.ActivityMapper;
import com.hyacinth.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.hyacinth.lotterysystem.dao.mapper.ActivityUserMapper;
import com.hyacinth.lotterysystem.service.activityStatus.ConvertActivityStatusDTO;
import com.hyacinth.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import com.hyacinth.lotterysystem.service.enums.ActivityStatusEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ActivityOperator extends AbstractActivityOperator {
    private static final Logger logger = LoggerFactory.getLogger(ActivityOperator.class);
    @Resource
    private ActivityMapper activityMapper;

    @Resource
    private ActivityUserMapper activityUserMapper;

    @Resource
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum activityStatusEnum = convertActivityStatusDTO.getActivityStatusEnum();
        if (activityId == null || activityStatusEnum == null)
            return false;

        ActivityDO activityDO = activityMapper.selectById(activityId);
        if (activityDO == null || activityStatusEnum.name().equalsIgnoreCase(activityDO.getStatus().name()))
            return false;

        // 需要判断奖品是否全部抽完
        LambdaQueryWrapper<ActivityPrizeDO> activityPrizeDOLambdaQueryWrapper = Wrappers.lambdaQuery(ActivityPrizeDO.class)
                .eq(ActivityPrizeDO::getActivityId, activityId)
                .eq(ActivityPrizeDO::getStatus, ActivityPrizeStatusEnum.INIT.name());
        Long count = activityPrizeMapper.selectCount(activityPrizeDOLambdaQueryWrapper);
        if (count > 0)
            return false;
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {

        LambdaUpdateWrapper<ActivityDO> activityDOLambdaUpdateWrapper = Wrappers.lambdaUpdate(ActivityDO.class)
                .set(ActivityDO::getStatus, convertActivityStatusDTO.getActivityStatusEnum().name())
                .eq(ActivityDO::getId, convertActivityStatusDTO.getActivityId());
        try {
            activityMapper.update(activityDOLambdaUpdateWrapper);
        } catch (Exception e) {
            return false;
        }
        logger.info("Activity 活动状态扭转了");
        return true;
    }
}
